package com.tushu.dao;

import com.tushu.model.Student;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class StudentDao extends BaseDAO{

    public List<Student> getStudentList() {
        String sql = "select * from tb_student";
        return super.selectAll(sql, new Object[]{});
    }

    public int addStudent(Student student) {
        String sql = "insert into tb_student values(null,?,?,?)";
        return super.update(sql, new Object[]{student.getAccount(),student.getPassword(),student.getName()});
    }

    public int deleteStudent(String account) {
        String sql = "delete from tb_student where account=?";
        return super.update(sql, new Object[]{account});
    }

    public int updateStudent(Student student) {
        String sql = "update tb_student set account=?,password=?,name=? where id=?";
        return super.update(sql, new Object[]{student.getAccount(),student.getPassword(),student.getName(),student.getId()});
    }

    public Student getStudentByAccount(String account) {
        String sql = "select * from tb_student where account=?";
        return super.select(sql, new Object[]{account});
    }



    @Override
    public Student rowMapper(ResultSet rs) throws SQLException {
        Student student = new Student();
        student.setId(rs.getInt("id"));
        student.setAccount(rs.getString("account"));
        student.setPassword(rs.getString("password"));
        student.setName(rs.getString("name"));
        return student;
    }
}
